package com.hua.yuan.live.repertory.cloud_data.loader;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hua.yuan.common.https.OkHttpModule;
import com.hua.yuan.live.repertory.cloud_data.JsonResult;
import com.hua.yuan.live.repertory.cloud_data.api.LiveReservationApi;
import com.hua.yuan.live.repertory.cloud_data.res.ResercationBean;
import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 直播预约的页面
 */
public class LiveGetReservationLoader extends AsyncTaskLoader<List<ResercationBean>> {

    private String mToken;
    private int mAnchorId;
    public LiveGetReservationLoader(Context context) {
        super(context);
    }

    @Override
    public List<ResercationBean> loadInBackground() {
        LiveReservationApi roomInfoApi=  OkHttpModule.getInstance().getRetrofit().create(LiveReservationApi.class);
        Call<String> call=roomInfoApi.getReservation(mToken,mAnchorId);
        try {
            Response<String> response = call.execute();
            if(response.body()==null || response.body().equals("")){
                return null;
            }
            JsonResult<JSONArray> resultData= JSONObject.parseObject(response.body(),JsonResult.class);
            if(resultData==null){
                return null;
            }
            JSONArray array=resultData.getData();
            List<ResercationBean> targetData = JSON.parseArray(array.toJSONString(),ResercationBean.class);
            return targetData;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 请求数据源
     * @param pToken
     * @param pAnchorId 主播的Id
     */
    public void requestData(String pToken,int pAnchorId){
        this.mToken=pToken;
        this.mAnchorId=pAnchorId;
    }

}
